﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ServiceProvider;

namespace ZBJF.Warranty.BLL
{
    public partial class ServiceProviderBranchService
    {

        #region 根据主键ID获取服务商网点 + ServiceProviderBranch LoadByID(int id)
        public ServiceProviderBranch LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断服务商网点是否存在+bool IsExist(string accessoriestName, int id = 0)
        /// <summary>
        /// 判断服务商网点是否存在
        /// </summary>
        /// <param name="branchName">服务商网点名称</param>
        /// <param name="id">服务商网点ID</param>
        /// <returns></returns>

        public bool IsExist(string branchName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderBranch>();
            if (!string.IsNullOrWhiteSpace(branchName))
                expressionWhere = expressionWhere.And(o => o.BranchName.Equals(branchName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取服务商网点列表 + IQueryable<ServiceProviderBranch> GetPagingList(ServiceProviderBranchModel model, out int total)
        /// <summary>
        /// 分页获取服务商网点列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<ServiceProviderBranch> GetPagingList(ServiceProviderBranchModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderBranch>();

            ////服务商类型
            //if (model.ServiceProviderTypeId > 0)
            //{
            //    expressionWhere = expressionWhere.And(o => o.ServiceProvider.ServiceProviderTypeId== model.ServiceProviderTypeId);
            //}

            //服务商网点类型
            if (model.ServiceType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ServiceProviderBranchServiceType.Where(t=>t.ServiceType== model.ServiceType).Any());
            }
            //所在省ID
            if (model.ProvinceId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ProvinceId == model.ProvinceId);
            }
            //所在市ID
            if (model.CityId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CityId == model.CityId);
            }

            //服务商名称
            if (!string.IsNullOrEmpty(model.ServiceProviderName))
            {
                string kw = model.ServiceProviderName.ToLower();
                expressionWhere = expressionWhere.And(o => o.ServiceProvider.ServiceProviderName.Contains(kw));
            }

            //服务商网点名称
            if (!string.IsNullOrEmpty(model.BranchName))
            {
                string kw = model.BranchName.ToLower();
                expressionWhere = expressionWhere.And(o => o.BranchName.Contains(kw));
            }

            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<ServiceProviderBranch>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion

        #region 获取服务商网点列表 + List<ServiceProviderBranchSelectModel> GetServiceProviderBranch(int serviceProviderId, byte warrantyMaintenanceTypeId, ServiceProductType serviceProductType)
        /// <summary>
        /// 获取服务商网点列表
        /// </summary>
        /// <param name="ServiceProviderId">服务商ID</param>
        /// <param name="WarrantyMaintenanceTypeId">服务类型ID</param>
        /// <param name="serviceProductType">服务商类型</param>
        /// <returns></returns>
        public List<ServiceProviderBranchSelectModel> GetServiceProviderBranch(int serviceProviderId, byte warrantyMaintenanceTypeId, ServiceProductType serviceProductType)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderBranchServiceType>();
            if (warrantyMaintenanceTypeId > 0)
                expressionWhere = expressionWhere.And(t => t.ServiceType == warrantyMaintenanceTypeId);

            var result = (from a in LoadEntities(t => t.IsAvailable == true && t.ServiceProviderId == serviceProviderId)
                         join b in ServiceFactory.ServiceProviderBranchServiceTypeServiceInstance.LoadEntities(expressionWhere) on a.ID equals b.ServiceProviderBranchID
                         join c in ServiceFactory.ServiceProviderTypeServiceInstance.LoadEntities(t=>t.ServiceProviderID== serviceProviderId && t.ServiceProviderTypeCode==(byte)serviceProductType) 
                                on b.ServiceProviderTypeID equals c.ID
                         select new ServiceProviderBranchSelectModel
                         {
                             BranchName = a.BranchName,
                             ID = a.ID
                         }).ToList();
            return result;
        }
        #endregion
        #region 获取服务商网点列表 + List<ServiceProviderBranchSelectModel> GetServiceProviderBranch(int serviceProviderId, byte warrantyMaintenanceTypeId, ServiceProductType serviceProductType)
        /// <summary>
        /// 获取服务商网点列表
        /// </summary>
        /// <param name="ServiceProviderId">服务商ID</param>
        /// <param name="WarrantyMaintenanceTypeId">服务类型ID</param>
        /// <returns></returns>
        public List<ServiceProviderBranchSelectModel> GetServiceProviderBranch(int serviceProviderId, byte warrantyMaintenanceTypeId)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProviderBranchServiceType>();
            if (warrantyMaintenanceTypeId > 0)
                expressionWhere = expressionWhere.And(t => t.ServiceType == warrantyMaintenanceTypeId);

            var result = (from a in LoadEntities(t => t.IsAvailable == true && t.ServiceProviderId == serviceProviderId)
                         join b in ServiceFactory.ServiceProviderBranchServiceTypeServiceInstance.LoadEntities(expressionWhere) on a.ID equals b.ServiceProviderBranchID
                         select new ServiceProviderBranchSelectModel
                         {
                             BranchName = a.BranchName,
                             ID = a.ID
                         }).ToList();
            return result;
        }
        #endregion
    }
}
